package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcAppraiser;
import com.csun.cmny.provider.model.dto.appraiser.AppraiserAuditDto;
import com.csun.cmny.provider.model.dto.appraiser.AppraiserQueryDto;
import com.csun.cmny.provider.model.dto.appraiser.AppraiserSaveDto;
import com.csun.cmny.provider.model.dto.appraiser.AppraiserUpdateDto;
import com.csun.cmny.provider.model.vo.appraiser.AppraiserQueryVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcAppraiserService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/appraiser")
public class OmcAppraiserController extends BaseController {

    @Resource
    private OmcAppraiserService appraiserService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增评估师")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addChanner(@RequestBody @Valid AppraiserSaveDto appraiserSaveDto, BindingResult br) {

        OmcAppraiser omcAppraiser = new OmcAppraiser();
        BeanUtils.copyProperties(appraiserSaveDto,omcAppraiser);
        omcAppraiser.setGroupId(getLoginAuthDto().getGroupId());
        omcAppraiser.setUserId(getLoginAuthDto().getUserId());
        omcAppraiser.setOperator(getLoginAuthDto().getUserName());
        int result = appraiserService.save(omcAppraiser);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "删除评估师")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Wrapper addChanner(Long id) {

        int delete = appraiserService.deleteByKey(id);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),delete);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改评估师")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper update(@RequestBody @Valid AppraiserUpdateDto appraiserUpdateDto, BindingResult br) {

        OmcAppraiser omcAppraiser = new OmcAppraiser();
        BeanUtils.copyProperties(appraiserUpdateDto,omcAppraiser);
        int update = appraiserService.update(omcAppraiser);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),update);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询评估师")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper update(@RequestBody  AppraiserQueryDto appraiserQueryDto, BindingResult br) {


        List<AppraiserQueryVo>list=appraiserService.selectQuery(appraiserQueryDto);
        PageInfo<AppraiserQueryVo> pageInfo = new PageInfo<>(list);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "审核评估师")
    @RequestMapping(value = "/audit", method = RequestMethod.POST)
    public Wrapper audit(@RequestBody @Valid AppraiserAuditDto appraiserAuditDto) {

        OmcAppraiser omcAppraiser = new OmcAppraiser();
        BeanUtils.copyProperties(appraiserAuditDto,omcAppraiser);
        int update = appraiserService.update(omcAppraiser);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),update);
    }
}
